package com.lanyou.cook.service;

import java.util.Arrays;
import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.lanyou.cook.common.Constants;
import com.lanyou.cook.datadictionary.DBType;
import com.lanyou.cook.datadictionary.InterfaceOperation;
import com.lanyou.cook.datadictionary.InterfaceScene;
import com.lanyou.cook.datadictionary.SystemInterfaceTecStandard;
import com.lanyou.cook.entity.domain.InterfaceVersionHistory;
import com.lanyou.cook.entity.domain.SystemInterface;
import com.lanyou.cook.repository.InterfaceVersionHistoryRepository;
import com.lanyou.cook.service.security.UserSessionService;

@Service
public class InterfaceVersionHistoryService {
	
	@Autowired
	private InterfaceVersionHistoryRepository interfaceVersionHistoryRepository;
	@Autowired
	private DataDictionaryService dataDictionaryService;
	
	@Transactional
	public void save(InterfaceOperation optType, String operator, boolean changeFromKettle, SystemInterface systemInterface){
		InterfaceVersionHistory interfaceVersionHistory = new InterfaceVersionHistory();
		
		if(operator == null){
			operator = UserSessionService.getCurrentAuthenticatedUserCode();
			if(operator == null){
				operator = Constants.APPLICATION_NAME2;
			}
		}
		
		interfaceVersionHistory.setOperator(operator);
		interfaceVersionHistory.setOperation(optType.ValueCode);
		interfaceVersionHistory.setCreateTime(new Date());
		interfaceVersionHistory.setChangeFromKettle(changeFromKettle);
		interfaceVersionHistory.setSystemInterfaceId(systemInterface.getId());
		interfaceVersionHistory.setSystemId(systemInterface.getOwnerFieldSystem().getId());
		interfaceVersionHistory.setKettleJobId(systemInterface.getKettleJob().getId());
		interfaceVersionHistory.setFieldName(systemInterface.getFieldName());
		interfaceVersionHistory.setFieldSystemName(systemInterface.getFieldSystemName());
		interfaceVersionHistory.setInterfaceName(systemInterface.getJobName());
		interfaceVersionHistory.setSenderFieldSystem(systemInterface.getSenderFieldSystemName());
		interfaceVersionHistory.setReceiverFieldSystem(systemInterface.getReceiverFieldSystemName());
		interfaceVersionHistory.setReceiverDBType(dataDictionaryService.getValueName(DBType.TypeCode, systemInterface.getReceiverDBType()) );
		interfaceVersionHistory.setSenderDBType(dataDictionaryService.getValueName(DBType.TypeCode,systemInterface.getSenderDBType()));
		interfaceVersionHistory.setInterfaceScene(dataDictionaryService.getValueName(InterfaceScene.TypeCode,systemInterface.getInterfaceScene()));
		interfaceVersionHistory.setTecStandard(dataDictionaryService.getValueName(SystemInterfaceTecStandard.TypeCode, systemInterface.getTecStandard()));
		interfaceVersionHistory.setInterfaceDesc(systemInterface.getInterfaceDesc());
		
		interfaceVersionHistoryRepository.save(Arrays.asList(interfaceVersionHistory));
	}
}